#!/usr/bin/perl

package Commands::Connection;

use parent 'Commands::BaseCommand';
use strict;
use warnings;
use JSON;
use Utils qw(dd);
use Consts;
use Carp qw(croak);
use Log;

require Consts;  # 显式导入所需变量

our $VERSION = '0.1';

our @actions = (
    {
        method => 'list',
        description => '列出连接信息',
    },
    {
        method => 'test',
        description => '执行连接操作',
        options => [
            {
                name => 'connection_name',
                description => '连接名称',
            },
        ]
    }
);

sub new
{
    my $class = shift;
    my $self = $class->SUPER::new();
    return $self;
}

sub list
{
    my $self = shift;
    my @args = @_;

    # 读取所有链接配置文件    
    my %connections;
    my $config_path = $Consts::CONFIG_PATH . $Consts::DS. 'connections';
    if (opendir(my $dir, $config_path)) {
        while (my $file = readdir($dir)) {
            if ($file =~ /\.json$/) {
                my $connection_name = $file;
                $connection_name =~ s/\.json$//;
                $connections{$connection_name} = _getConfigByName($connection_name);
            }
        }
        closedir($dir);
    }

    # 打印连接信息
    Log::info("Connections list:");
    for my $connection_name (sort keys %connections) {
        Log::primary("Connection [${connection_name}]");
        my $config = $connections{$connection_name};

        for my $key (keys %$config) {
            Log::info("   $key: $config->{$key}");
        }

        my $command = _getDB($self, $config)->getCommand();
        Log::info("   Command: [${command}]");
    }
}

sub test
{
    my $self = shift;
    my @args = @_;

    my $connection_name = $args[0];
    unless (defined $connection_name) {
        Log::error("Missing option: connection_name.");
        $self->SUPER::usage();
        return;
    }
    
    my $config = _getConfigByName($connection_name);
    unless (defined $config) {
        return;
    }
    Log::primary("Connection [${connection_name}]");
    for my $key (keys %$config) {
        Log::info("   $key: $config->{$key}");
    }

    Log::info("Test connection ...");
    my $db = _getDB($self, $config);
    $db->connectDB();
    my $version = $db->getVersion();
    Log::success("Connection success. version: ${version}.");
}

sub _getConfigByName
{
    my $connection_name = shift;
    my $config_path = $Consts::CONFIG_PATH. $Consts::DS. 'connections'. $Consts::DS. "${connection_name}.json";
    unless (-f $config_path) {
        Log::error("Connection config [${connection_name}] not found.");
        return;
    }
    open(my $fh, '<', $config_path) or croak "Failed to open file: $!";
    my $content = do { local $/; <$fh> };
    close($fh);
    my $config = JSON->new->decode($content);
    return $config;
}

sub _getDB
{
    my $self = shift;
    my $config = shift;

    my $db_module_path = $Consts::LIB_PATH. $Consts::DS. 'DB'. $Consts::DS. $config->{type}. '.pm';
    my $db_module = "DB::" . $config->{type};
    require $db_module_path;
    return $db_module->new(\%$config);
}

1;
